/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.execute.controller;


import com.icinfo.cloud.common.core.Validator.BaseGroup;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.common.caseregister.service.IRegisterCommonService;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.DocumentCommonFormDataVo;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.DocumentCommonFormHtmlVo;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteDocCommonHtmlRequest;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteDocCommonOperateSaveDto;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteDocumentRecordDelDto;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteTemplateCodeInfoDto;
import com.icinfo.cloud.provider.punish.execute.service.IExecuteDocCommonOperateService;
import com.icinfo.cloud.provider.punish.execute.service.IExecuteDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.caseregister.dto.CaseRegisterSaveDto;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import com.icinfo.framework.tools.utils.MapUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 裁执分离文书公共操作控制类
 * @author liyafeng
 * @date 2022/8/16
 */
@RestController
@RefreshScope
@RequestMapping("/punish/execute/doc")
@Slf4j
public class ExecuteDocCommonOperateController extends BaseController {
    @Resource
    private IExecuteDocCommonOperateService executeDocCommonOperateService;
    @Resource
    private IExecuteDocumentRecordService executeDocumentRecordService;
    @Resource
    private IRegisterCommonService commonService;
    /**
     * 测试用
     *
     * @param saveDto 保存dto
     * @return {@link Result<Map<String, Object>> }
     * @author shishengyao
     * @date 2022/05/28
     */
    @PostMapping(value = "/testDoc")
    public Result<String> casePreview(@RequestBody @Validated CaseRegisterSaveDto saveDto) {
        UserDTO userInfo = UserUtil.getUserInfo();
        ExecuteDocCommonOperateSaveDto docCommonSaveDto =new ExecuteDocCommonOperateSaveDto();
        docCommonSaveDto.setExecuteId("1");
        docCommonSaveDto.setDocumentCatalogCode("QZZXTZSCSF1862");
        docCommonSaveDto.setIsUpdateRelationTable(Constant.STRING_0);
        docCommonSaveDto.setDocumentTemplateType("1");
        //saveDto.setPartyId(saveDto.getPartyIds());
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("orgStandardName",commonService.getOrgStandardNameByOrgCode(userInfo.getOrgCode(),userInfo.getDeptCode()));
        dataMap.put("userName","王五");
        dataMap.put("content","存在违反该规定情况");
        dataMap.put("evidenceContent","充分依据及证明");
        dataMap.put("xingwei","存在的");
        dataMap.put("guiding",commonService.getOrgStandardNameByOrgCode(userInfo.getOrgCode(),userInfo.getDeptCode()));
        dataMap.put("criminalContent","行政处罚");
        docCommonSaveDto.setDataMap(dataMap);
        Map<String, Object> docResultMap = executeDocCommonOperateService.saveDocInfo(docCommonSaveDto);
        String status = MapUtils.getString(docResultMap, "success", "");
        String msg = MapUtils.getString(docResultMap, "msg", "");
        if (!"true".equals(status)) {
            log.info("----------文书生成公共服务saveDocInfo------报错---" + docResultMap);
            throw new BusinessException(msg);
        }
        //根据文书目录编码获取模板code
        return Result.success("成功");
    }
    /**
     * 【V1.4.2】描述：删除文书记录(裁执分离)
     * @author wuzhihui
     * @date 2022/9/13
     * @param executeDocumentRecordDelRequest
     * @return {@link Result< String>}
     */
    @PostMapping("/delExecuteDocumentRecordInfo")
    public Result<String> delExecuteDocumentRecordInfo(@RequestBody @Validated ExecuteDocumentRecordDelDto executeDocumentRecordDelRequest) {
        int num=executeDocumentRecordService.delExecuteDocumentRecordInfo(executeDocumentRecordDelRequest);
        if(num<1){
            Result.error("删除失败");
        }
        return Result.success("删除成功");
    }
    /**
     * 描述：案源保存文书信息通用接口
     * @author liyafeng
     * @date 2022/8/16
      * @param saveDto
     * @return {@link Result< String>}
     */
    @PostMapping("/saveExecuteDocInfo")
    @ApiOperation(value = "【V1.4.2】描述：保存文书信息通用接口", httpMethod = "POST")
    public Result<Map<String, Object>> saveExecuteDocInfo(@RequestBody @Validated ExecuteDocCommonOperateSaveDto saveDto) {
        Map<String, Object> resultMap = executeDocCommonOperateService.saveDocInfo(saveDto);
        String status = MapUtils.getString(resultMap, "success", "");
        if ("true".equals(status)) {
            String documentId = MapUtils.getString(resultMap, "documentId", "");
            return Result.success(resultMap);
        }
        return Result.error(Constant.STRING_SAVE_FAIL);
    }

    /**
     * 获取裁执分离文书html公共方法
     * @author liyafeng
     * @date 2022/8/16
      * @param request
     * @return {@link Result< DocumentCommonFormHtmlVo>}
     */
    @PostMapping("/getExecuteDocBaseInfo")
    public Result<DocumentCommonFormHtmlVo> getDocHtml(@RequestBody @Validated(BaseGroup.Query.class) ExecuteDocCommonHtmlRequest request) {
        DocumentCommonFormHtmlVo result = executeDocCommonOperateService.getDocumentFormHtml(request);
        return Result.success(Constant.STRING_GET_SUCCESS, result);
    }


    /**
     * 获取文书数据公共方法
     * @author liyafeng
     * @date 2022/8/26
      * @param request
     * @return {@link Result< DocumentCommonFormDataVo>}
     */
    @PostMapping("/getExecuteDocFormData")
    public Result<DocumentCommonFormDataVo> getDocFormData(@RequestBody @Validated(BaseGroup.Query.class) ExecuteDocCommonHtmlRequest request) {
        DocumentCommonFormDataVo result = executeDocCommonOperateService.getCommonDocumentFormData(request);
        return Result.success(Constant.STRING_GET_SUCCESS, result);
    }


    /**
     * 描述：根据文书目录编码获取法人或个人文件模板code
     * @author wuzhihui
     * @date 2022/8/26
      * @param dto
     * @return {@link Result< String>}
     */
    @PostMapping("/getTemplateCodeByDocumentCatalogCode")
    @ApiOperation(value = "【V1.4.2】描述：根据文书目录编码获取法人或个人文件模板code", httpMethod = "POST")
    public Result<String> getTemplateCodeByDocumentCatalogCode(@Validated @RequestBody ExecuteTemplateCodeInfoDto dto) {
        String documentCatalogCode = dto.getDocumentCatalogCode();
        String executeId = dto.getExecuteId();
        Map<String, Object> paramMap = dto.getParamMap();
        String templateCode = executeDocCommonOperateService.getTemplateCodeByDocumentCatalogCode(documentCatalogCode, executeId, paramMap);
        return Result.success(Constant.STRING_GET_SUCCESS, templateCode);
    }

}
